<?php

namespace App\Admin\Controllers;

use App\Models\MoneyLog;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class MoneyLogController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('余额记录')
            ->description('余额记录')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new MoneyLog);

        $grid->id('Id');
        $grid->user()->phone('手机号');
        $grid->money_log_type()->name('变动类型');
        // $grid->type_name('变动类型');
        $grid->status('状态')->display(function($value){
            return ($value == 1)?"<span class='label label-success'>增加</span>":"<span class='label label-error'>减少</span>";
        });
        $grid->before('变动前');
        $grid->after('变动后');
        $grid->change_money('变动金额');
        $grid->created_at('创建时间');
        $grid->updated_at('更新时间');
        $grid->disableActions();
        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disableCreateButton();
        $grid->model()->orderBy('id', 'desc');
        $grid->actions(function ($actions) {
            $actions->disableView();
            $actions->disableEdit();
            $actions->disableDelete();
        });
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->equal('type_id','类型')->select('/admin/get_money_log_type');
            $filter->where(function ($query) {
                    $query->whereHas('user', function ($query) {
                        $query->where('phone', $this->input);
                    });
            }, '手机号码');
            $filter->between('created_at', '日期')->datetime();
            $filter->scope('status', '增长')->where('status', 1);
            $filter->scope('status', '减少')->where('status', 1);
            
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(MoneyLog::findOrFail($id));

        $show->id('Id');
        $show->type_id('Type id');
        $show->type_name('Type name');
        $show->status('Status');
        $show->before('Before');
        $show->after('After');
        $show->change_money('Change money');
        $show->created_at('Created at');
        $show->updated_at('Updated at');
        
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new MoneyLog);

        $form->number('type_id', 'Type id');
        $form->text('type_name', 'Type name');
        $form->switch('status', 'Status');
        $form->decimal('before', 'Before');
        $form->decimal('after', 'After');
        $form->decimal('change_money', 'Change money');

        return $form;
    }
}
